package pl.gigiel.walker.main;

import pl.gigiel.behaviour.IBoidFactory;
import pl.gigiel.behaviour.IBoidFeatures;
import pl.gigiel.behaviour.IEnvironment;
import pl.gigiel.behaviour.IEnvironmentFactory;
import pl.gigiel.behaviour.ISteerFactory;
import pl.gigiel.behaviour.ISteerManager;
import pl.gigiel.behaviour.ISteerManagerFactory;
import pl.gigiel.behaviour.environment.SimpleEnvironment;
import pl.gigiel.behaviour.environment.factory.EnvironmentFactory;
import pl.gigiel.behaviour.exception.BehaviourException;
import pl.gigiel.behaviour.steer.AlignmentSteer;
import pl.gigiel.behaviour.steer.CohesionSteer;
import pl.gigiel.behaviour.steer.ForwardSteer;
import pl.gigiel.behaviour.steer.SeekSteer;
import pl.gigiel.behaviour.steer.SeparationSteer;
import pl.gigiel.behaviour.steer.WanderSteer;
import pl.gigiel.behaviour.steer.factory.SteerFactory;
import pl.gigiel.behaviour.steermanager.SimpleSteerManager;
import pl.gigiel.behaviour.steermanager.factory.SteerManagerFactory;
import pl.gigiel.behaviour.util.RandomDirectionGenerator;
import pl.gigiel.behaviour.util.Regular2DPositionGenerator;
import pl.gigiel.jme.behaviour.IBoidBodyFactory;
import pl.gigiel.jme.behaviour.boidbody.SimpleBoidBody;
import pl.gigiel.jme.behaviour.boidbody.SprinterBoid;
import pl.gigiel.jme.behaviour.boidbody.factory.BoidBodyFactory;
import plgigiel.behaviour.boid.BoidFeatures;
import plgigiel.behaviour.boid.SimpleBoid;
import plgigiel.behaviour.boid.factory.BoidFactory;

import com.jme.app.SimpleGame;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;

public class WalkerMain extends SimpleGame {
	public static void main(String[] args) {
		WalkerMain walkerApp = new WalkerMain();
		walkerApp.setConfigShowMode(ConfigShowMode.ShowIfNoConfig);
		walkerApp.start();
	}

	private int boidCounter;

	// @Override
	protected void simpleInitGame2() {
		new SprinterBoid(rootNode);
		cam.getLocation().set(0, 0, 100);
		cam.update();
	}

	@Override
	protected void simpleInitGame() {
		Vector3f cornerMin = new Vector3f(-20f, -20f, -20f);
		Vector3f cornerMax = new Vector3f(20f, 20f, 20f);
		Vector3f position = new Vector3f(1f, 1f, 1f);
		Vector3f direction = new Vector3f(-1f, 1f, 1f);
		Vector3f velocity = new Vector3f(1f, 1f, 1f);
		float speedMax = 20f;
		Vector3f size = new Vector3f(1f, 2f, 1f);
		float mass = 1f;
		ColorRGBA color = null;
		try {
			IEnvironmentFactory environmentFactory = EnvironmentFactory.getInstance();
			IEnvironment environment = environmentFactory.createEnvironment(SimpleEnvironment.class);
			// environment.setBorder(cornerMin, cornerMax);
			//
			ISteerFactory steerFactory = SteerFactory.getInstance();
			ForwardSteer forwardSteer = (ForwardSteer) steerFactory.createSteer(ForwardSteer.class);
			WanderSteer wanderSteer = (WanderSteer) steerFactory.createSteer(WanderSteer.class);
			AlignmentSteer alignmentSteer = (AlignmentSteer) steerFactory.createSteer(AlignmentSteer.class);
			CohesionSteer cohesionSteer = (CohesionSteer) steerFactory.createSteer(CohesionSteer.class);
			SeparationSteer separationSteer = (SeparationSteer) steerFactory.createSteer(SeparationSteer.class);
			SeekSteer seekSteer = (SeekSteer) steerFactory.createSteer(SeekSteer.class);
			//
			ISteerManagerFactory steerManagerFactory = SteerManagerFactory.getInstance();
			ISteerManager steerManager = steerManagerFactory.createSteerManager(SimpleSteerManager.class);
			// steerManager.add(forwardSteer, 10f, 1f, 0.005f);
			// steerManager.add(wanderSteer, 20f, 1f, 0.005f);
			//steerManager.add(alignmentSteer, 10f, 1f, 0.01f);
			steerManager.add(cohesionSteer, 10f, 1f, 1f);
			steerManager.add(separationSteer, 10f, 1f, 1f);
			//steerManager.add(seekSteer, 20f, 1f, 1f);
			//
			IBoidFactory boidFactory = BoidFactory.getInstance();
			IBoidBodyFactory boidBodyFactory = BoidBodyFactory.getInstance(rootNode, boidFactory);
			for (int i = 0; i < 33; i++) {
				position = Regular2DPositionGenerator.position(size, cornerMin, cornerMax);
				direction = RandomDirectionGenerator.direction();
				color = ColorRGBA.randomColor();

				IBoidFeatures features = new BoidFeatures();
				String id = SimpleBoidBody.class.getName() + Integer.toString(++boidCounter);

				features.setId(id);
				features.setMass(mass);
				features.setSize(size);
				features.setColor(color);
				features.setAgility(0.5f);
				features.setSpeed(10.0f);
				features.setStamina(1.0f);
				features.setPosition(position);
				features.setVelocity(velocity);

				boidBodyFactory.createBoidBody(SimpleBoid.class, SimpleBoidBody.class, environment, steerManager, features);
			}
			// boidFactory.createBoid(SimpleBoid.class, environment, steerManager, RandomPositionGenerator.position(sizeBoid, cornerMin, cornerMax), sizeBoid, Vector3f.UNIT_X, ColorRGBA.red);
			// boidFactory.createBoid(SimpleBoid.class, environment, steerManager, RandomPositionGenerator.position(sizeBoid, cornerMin, cornerMax), sizeBoid, Vector3f.UNIT_X, ColorRGBA.red);
			// boidFactory.createBoid(SimpleBoid.class, environment, steerManager, RandomPositionGenerator.position(sizeBoid, cornerMin, cornerMax), sizeBoid, Vector3f.UNIT_X, ColorRGBA.red);
			// boidFactory.createBoid(SimpleBoid.class, environment, steerManager, RandomPositionGenerator.position(sizeBoid, cornerMin, cornerMax), sizeBoid, Vector3f.UNIT_Y, ColorRGBA.green);
			// boidFactory.createBoid(Buoy.class, environment, null, cornerMin,
			// sizeBoid, directionBoid, ColorRGBA.blue);
			// boidFactory.createBoid(Buoy.class, environment, null, cornerMax,
			// sizeBoid, directionBoid, ColorRGBA.green);
		} catch (BehaviourException e) {
			e.printStackTrace();
		}
		cam.getLocation().set(0, 0, 100);
		cam.update();
	}
}
